package com.star.jersey.mybatisplus.base.core.contract.exception;

import com.star.jersey.mybatisplus.base.core.contract.enums.ResultCode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zhouhengzhe
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ServiceException extends RuntimeException {
    private static final long serialVersionUID = 1026631908939212210L;
    protected Integer errCode;
    protected String msg;

    public ServiceException() {
        this(ResultCode.FAIL.getCode(), ResultCode.FAIL.getDesc());
    }

    public ServiceException(String msg) {
        this(ResultCode.FAIL.getCode(), msg);
    }

    public ServiceException(String msg, Throwable e) {
        super(msg, e);
        this.errCode = ResultCode.FAIL.getCode();
        this.msg = msg;
    }

    public ServiceException(ResultCode resultCode) {
        this(resultCode.getCode(), (String) resultCode.getDesc(), (Throwable) null);
    }

    public ServiceException(Integer errCode, String msg) {
        this(errCode, (String) msg, (Throwable) null);
    }

    public ServiceException(Integer errCode, String msg, Throwable cause) {
        super(msg, cause);
        this.errCode = errCode;
        this.msg = msg;
    }

    public ServiceException(Integer code, String... params) {
        this.errCode = code;
        this.msg = StringUtils.join(params);
    }

    public ServiceException(Integer code, Throwable e) {
        super(e);
        this.errCode = code;
        this.msg = e.getMessage();
    }

    public ServiceException(Integer code, Throwable e, String... params) {
        super(e);
        this.errCode = code;
        this.msg = StringUtils.join(params);
    }

}
